package 线程;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadDemo1 {
    public static int i = 1;
    public volatile static int j = 1;
    public volatile static boolean flag = false;
    public static int count = 0;

    public static Lock lock = new ReentrantLock();
    public static Condition conditionA = lock.newCondition();
    public static Condition conditionB = lock.newCondition();

    private static CountDownLatch latch = new CountDownLatch(2);
    private static AtomicInteger numA = new AtomicInteger();
    private static AtomicInteger numB = new AtomicInteger();

    private static byte[] block = new byte[0];

    public static void main(String[] args) {
        new Thread(new Runnable() {
            public void run() {
                while (i <= 100) {
                    synchronized (block) {
                        if (flag) {
                            try {
                                block.wait();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            System.out.println(Thread.currentThread().getName()
                                    + "线程1----" + (i++));
                            flag = true;
                            block.notifyAll();
                        }

                    }
                }
            }
        }).start();

//        new Thread(() -> {
//            while (i <= 100) {
//                synchronized (block) {
//
//                    if (!flag) {
//                        try {
//                            block.wait();
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    } else {
//                        System.out.println(Thread.currentThread().getName()
//                                + "线程2----" + (i++));
//                        flag = false;
//                        block.notifyAll();
//                    }
//
//                }
//            }
//        }).start();

    }
}